import BaseController from "./base.js";
/**
 * 二手房相关后台接口
 * 
 */
export default class extends BaseController {
    /**
    * 获取二手房列表的数据
    *
    * @returns {Promise} 包含满足条件的二手房列表的Promise对象
    * @example
    *
    * const secondListInfo = await request.post("/api/admin/secondhouse/list");
    * 返回列表每一个对象包含以下属性：
    * "errno": 0,
        "errmsg": "",
        "data": [
            {
                "id": 1,
                "domain": "海珠",
                "subway": "中大",
                "address": "海联路51号501",
                "img": "11",
                "community": "海联路大院",
                "price": 2222,
                "area": "40",
                "public_area": null,
                "layout": "两室一厅",
                "direction": null,
                "sale_time": null,
                "householder": "刘",
                "announce_time": "2021-06-15 00:00:00",
                "priority": "0"
            }
        ]
    }
    */
   async listAction(){
        //定义变量保存查找后的数据库数据
        let rows = await this.model("secondhouse")
            .select();
        
        //判断数据库查找是否成功
        if(!think.isEmpty(rows)){
            return this.success(rows);
        }else{
            return this.fail(1000,'数据错误');
        }
   }

    /**
    * 修改二手房数据
    * @param {int} id
    * @param {string} domain
    * @param {string} subway
    * @param {string} address
    * @param {string} img
    * @param {string} community
    * @param {float} price
    * @param {float} area
    * @param {string} layout
    * @param {string} direction
    * @param {datetime} sale_time
    * @param {string} householder
    * @param {datetime} announce_time
    * @param {string} priority 
    *
    * @returns {Promise} 包含创建信息
    * @example
    *
    * const secondhouseUpdateInfo = await request.post("/api/admin/secondhouse/update");
    * 返回列表每一个对象包含以下属性：
    * {
        "errno": 0,
        "errmsg": "",
        "data": "修改成功"
      }
    */
    async updateAction(){
        //定义变量保存前端传入参数
        // let id = this.post("id");
        // let domain = this.post("domain");
        // let subway = this.post("subway");
        // let address = this.post("address");
        // let img = this.post("img");
        // let community = this.post("community");
        // let price = this.post("price");
        // let area = this.post("area");
        // let layout = this.post("layout");
        // let direction = this.post("direction");
        // let householder = this.post("householder");
        // let announce_time = this.post("announce_time");
        // let priority = this.post("priority");

        if(!think.isEmpty(this.post()['houseList'])){
            if(think.isEmpty(this.post()['houseList']['id'])){
                return this.fail(1000,"传参错误");
            }
            var data = this.post()['houseList'];
        }else{
            return this.fail(1000,"传参错误");
        }

        
        // if(!think.isEmpty(data['householder'])){
        //     let householder = await this.model("manager")
        //     .where({id:householder})
        //     .find();

        //     if(think.isEmpty(householder)){
        //         return this.fail(1000,"无此托管人");
        //     }
        // }

        //定义变量保存数据库操作信息
        let info = await this.model("secondhouse")
            .where({id : data['id']})
            .update(data);
        
        //判断数据库更新是否成功
        if(!think.isEmpty(info)){
            return this.success("修改成功");
        }else{
            return this.fail(1000,'修改失败');
        }
    }

    /**
    * 添加二手房数据
    * @param {int} id
    * @param {string} domain
    * @param {string} subway
    * @param {string} address
    * @param {string} img
    * @param {string} community
    * @param {float} price
    * @param {float} area
    * @param {string} layout
    * @param {string} direction
    * @param {datetime} sale_time
    * @param {string} householder
    * @param {datetime} announce_time
    * @param {string} priority 
    *
    * @returns {Promise} 包含添加信息
    * @example
    *
    * const secondhouseAddInfo = await request.post("/api/admin/secondhouse/add");
    * 返回列表每一个对象包含以下属性：
    * {
        "errno": 0,
        "errmsg": "",
        "data": "添加成功"
      }
    */
    async addAction(){

        if(!think.isEmpty(this.post()['houseList'])){
            var data = this.post()['houseList'];
        }else{
            return this.fail(1000,"传参错误");
        }

        //设置发布时间为当前
        let date = require("silly-datetime");
    	let now_date = date.format(new Date() , 'YYYY-MM-DD');
        data['announce_time'] = now_date;

        // if(!think.isEmpty(data['householder'])){
        //     let holder = await this.model('manager')
        //     .where({id : data['householder']})
        //     .find();

        //     if(think.isEmpty(holder)){
        //         return this.fail(1000,'无此托管人');
        //     }
        // }

        data['sale_time'] = data['sale_time'].substring(0,10);
        //定义变量保存数据库执行信息
        let info = await this.model("secondhouse")
            .add(data);
        
        //判断数据库查找是否成功
        if(!think.isEmpty(info)){
            return this.success("添加成功");
        }else{
            return this.fail(1000,'添加失败');
        }
    }

    /**
    * 删除二手房数据
    * @param {int} id 需删除的二手房id
    *
    * @returns {Promise} 包含操作提示语句
    * @example
    *
    * const secondhouseDeleteInfo = await request.post("/api/admin/secondhouse/delete");
    * 返回列表每一个对象包含以下属性：
    * {
        "errno": 0,
        "errmsg": "",
        "data": "删除成功"
      }
    */
    async deleteAction(){
        //定义变量保存前端传过来的id
        if(!think.isEmpty(this.post("id"))){
            var id = this.post("id");
        }else{
            return this.fail(1000,'传参错误');
        }
        
        let info = await this.model("secondhouse").where({id : id}).delete();
        
        if(!think.isEmpty(info)){
            return this.success("删除成功");
        }else{
            return this.fail(1000,'删除失败');
        }
    }
}